package com.dc.modules.main.controller;


import javax.servlet.http.HttpSession;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.dc.commons.base.controller.DcBaseController;
import com.dc.commons.shiro.defaults.DefaultMenu;
import com.dc.commons.views.ModelAndViewBuilder;
import com.google.common.collect.Lists;
/*******************************************************************************<P>
 * 文件名: MainController.java <br>
 * 版本: 1.0<br>
 * 描述: Main<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2015年12月16日 上午10:14:22<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 *****************************************************************************
 */
@Controller
@RequestMapping("/main")
public class MainController extends DcBaseController {

	/**
	 * 登录页面
	 * @return ModelAndView
	 */
	@RequestMapping(path="login")
	public ModelAndView login(NativeWebRequest request){
		String exceptionClassName = (String)request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME,NativeWebRequest.SCOPE_REQUEST);
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        }else if(AuthenticationException.class.getName().equals(exceptionClassName)){
        	 error = "用户名/密码错误";
        } else if(DisabledAccountException.class.getName().equals(exceptionClassName)){
        	error = "账号已经失效！";
        }else if(LockedAccountException.class.getName().equals(exceptionClassName)){
        	error = "账号已锁定，请于管理员联系!";
        }else if(ExpiredCredentialsException.class.getName().equals(exceptionClassName)){
        	error = "会话已经失效，请重新登录！";
        }else if(ExcessiveAttemptsException.class.getName().equals(exceptionClassName)){
        	error = "登录错误次数过多，请稍后再试！";
        }else if(exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }
		
		return ModelAndViewBuilder.create()
				.viewName("main/login")
				.error(error)
				.build();
	}
	
	
	/**
	 * 首页
	 * @return ModelAndView
	 */
	@RequestMapping(path="index")
	@RequiresUser
	public ModelAndView index(){
		
		DefaultMenu menu=new DefaultMenu();
		menu.setCode("");
		menu.setName("test");
		menu.setUrl("/test/simple/index.htm");
		return ModelAndViewBuilder.create()
				.success()
				.viewName("main/index")
				.result(Lists.newArrayList(menu))
				.build();
	}
	
	/**
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping(path="welcome")
	@RequiresUser
	public ModelAndView welcome(){
		return new ModelAndView("main/welcome");
	}
	
	@RequestMapping(path="testRole")
	@RequiresRoles("admin")
	public ModelAndView testRole(){
		return new ModelAndView("main/welcome");
	}
	
	
	/**
	 * 动态控制日志的输出级别为debug
	 * @param session
	 * @return ModelAndView
	 */
	@RequestMapping(path="debug")
	public ModelAndView debug(HttpSession session){
		session.setAttribute("logBack", "debug");
		return ModelAndViewBuilder
				.create()
				.success()
				.build();
	}
	
	/**
	 * 动态控制日志级别，将输入级别回复为原定的级别
	 * @param session
	 * @return ModelAndView
	 */
	@RequestMapping(path="unDebug")
	public ModelAndView unDebug(HttpSession session){
		
		session.removeAttribute("logBack");
		return ModelAndViewBuilder
				.create()
				.success()
				.build();
	}
	
	
	
}
